package com.vanke.agent;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONObject;

import com.common.api.handler.MappingHandler;
import com.vanke.agent.bean.CstInfo;
import com.vanke.agent.bean.RoomInfo;
import com.vanke.agent.common.Cache;
import com.vanke.agent.common.Util;
import com.vanke.agent.sync.CustomerSync;

public class Test {
	static CustomerSync cs = null;//new CustomerSync();

	static String getSex(int gender) {
		switch (gender) {
		case 1:
			return "男";
		case 2:
			return "女";
		default:
			return "未知";
		}
	}

	static int conditions = 0;

	static void doit(String cdesc, CstInfo c, CstInfo old) {
		conditions++;
		System.out.println(String.format("场景%02d：%s", conditions, cdesc));
		System.out.println(String.format(
				"A[姓名：%s,性别：%s,身份证：%s,手机：%s,修改时间：%s,明源GUID：%s]",
				c.getCstName(), getSex(c.getGender()), c.getCardId(),
				c.getMobile(), Util.formatDate(c.getLastModifyTime()),
				c.getOriGUID()));
		System.out.println(String.format(
				"D[姓名：%s,性别：%s,身份证：%s,手机：%s,修改时间：%s,明源GUID：%s]",
				old.getCstName(), getSex(old.getGender()), old.getCardId(),
				old.getMobile(), Util.formatDate(old.getLastModifyTime()),
				old.getOriGUID()));
		try {
			boolean merge = cs.shouldMerge("Test", c, old);
			System.out.println(merge ? "合并" : "不合并，保存冲突");
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.print("-------------------------------");
		System.out.print("-------------------------------");
		System.out.println("-------------------------------");
	}

	private static void testNoGUIDHasCardAHasCardNameNotSame() {
		CstInfo c = new CstInfo();
		c.setMobile("13812345678");
		c.setCardId("350722198812310016");
		c.setCstName("李三儿");
		c.setGender(1);
		c.setOriGUID("0C364EFF-D720-4E69-BD79-280A40A73F70");
		c.setLastModifyTime(new Timestamp(System.currentTimeMillis()));

		CstInfo old = new CstInfo();
		old.setLastModifyTime(new Timestamp(System.currentTimeMillis()));
		old.setMobile("13812345678");
		old.setCardId("511025198603024066");
		old.setCstName("张先生");
		old.setGender(1);

		doit("Ｄ没有明源GUID，D有身份证,A有身份证，姓氏不同", c, old);
	}

	private static void testNoGUIDHasCardAHasCardNameSame() {
		CstInfo c = new CstInfo();
		c.setMobile("13812345678");
		c.setCardId("350722198812310016");
		c.setCstName("张三儿");
		c.setGender(1);
		c.setOriGUID("0C364EFF-D720-4E69-BD79-280A40A73F70");
		c.setLastModifyTime(new Timestamp(System.currentTimeMillis()));

		CstInfo old = new CstInfo();
		old.setLastModifyTime(new Timestamp(System.currentTimeMillis()));
		old.setMobile("13812345678");
		old.setCardId("511025198603024066");
		old.setCstName("张先生");
		old.setGender(1);

		doit("Ｄ没有明源GUID，D有身份证,A有身份证，姓氏相同", c, old);
	}

	private static void testNoGUIDHasCardANoCardNameSame() {
		CstInfo c = new CstInfo();
		c.setMobile("13812345678");
		c.setCardId("");
		c.setCstName("张三儿");
		c.setOriGUID("0C364EFF-D720-4E69-BD79-280A40A73F70");
		c.setLastModifyTime(new Timestamp(System.currentTimeMillis()));

		CstInfo old = new CstInfo();
		old.setLastModifyTime(new Timestamp(System.currentTimeMillis()));
		old.setMobile("13812345678");
		old.setCardId("511025198603024066");
		old.setCstName("张先生");

		doit("Ｄ没有明源GUID，D有身份证,A没有身份证，姓氏相同", c, old);
	}

	private static void testNoGUIDHasCardANoCardNameNotSame() {
		CstInfo c = new CstInfo();
		c.setMobile("13812345678");
		c.setCardId("");
		c.setCstName("李三儿");
		c.setOriGUID("0C364EFF-D720-4E69-BD79-280A40A73F70");
		c.setLastModifyTime(new Timestamp(System.currentTimeMillis()));

		CstInfo old = new CstInfo();
		old.setLastModifyTime(new Timestamp(System.currentTimeMillis()));
		old.setMobile("13812345678");
		old.setCardId("511025198603024066");
		old.setCstName("张先生");

		doit("Ｄ没有明源GUID，D有身份证,A没有身份证，姓氏不同", c, old);
	}

	private static void testNoGUIDNoCardANoCardNameNotSame() {

		CstInfo c = new CstInfo();
		c.setMobile("13812345678");
		c.setCardId("");
		c.setGender(1);
		c.setCstName("李三儿");
		c.setOriGUID("0C364EFF-D720-4E69-BD79-280A40A73F70");
		c.setLastModifyTime(new Timestamp(System.currentTimeMillis()));

		CstInfo old = new CstInfo();
		old.setLastModifyTime(new Timestamp(System.currentTimeMillis()));
		old.setMobile("13812345678");
		old.setCardId("");
		old.setGender(1);
		old.setCstName("张先生");

		doit("Ｄ没有明源GUID，没有身份证；A没有身份证,姓氏不同", c, old);
	}

	private static void testNoGUIDNoCardANoCardNameSame() {

		CstInfo c = new CstInfo();
		c.setMobile("13812345678");
		c.setCardId("");
		c.setCstName("张三");
		c.setOriGUID("0C364EFF-D720-4E69-BD79-280A40A73F70");
		c.setLastModifyTime(new Timestamp(System.currentTimeMillis()));

		CstInfo old = new CstInfo();
		old.setLastModifyTime(new Timestamp(System.currentTimeMillis()));
		old.setMobile("13812345678");
		old.setCardId("");
		old.setCstName("张先生");

		doit("Ｄ没有明源GUID，没有身份证；A没有身份证,姓氏相同", c, old);
	}

	private static void testNoGUIDNoCardAHasCardNameNotSame() {

		CstInfo c = new CstInfo();
		c.setMobile("13812345678");
		c.setCardId("511025198603024066");
		c.setGender(1);
		c.setCstName("李三儿");
		c.setOriGUID("0C364EFF-D720-4E69-BD79-280A40A73F70");
		c.setLastModifyTime(new Timestamp(System.currentTimeMillis()));

		CstInfo old = new CstInfo();
		old.setLastModifyTime(new Timestamp(System.currentTimeMillis()));
		old.setMobile("13812345678");
		old.setCardId("");
		old.setGender(1);
		old.setCstName("张先生");

		doit("Ｄ没有明源GUID，没有身份证；A有身份证,姓氏不同", c, old);
	}

	private static void testNoGUIDNoCardAHasCardNameSame() {

		CstInfo c = new CstInfo();
		c.setMobile("13812345678");
		c.setCardId("511025198603024066");
		c.setCstName("张三");
		c.setOriGUID("0C364EFF-D720-4E69-BD79-280A40A73F70");
		c.setLastModifyTime(new Timestamp(System.currentTimeMillis()));

		CstInfo old = new CstInfo();
		old.setLastModifyTime(new Timestamp(System.currentTimeMillis()));
		old.setMobile("13812345678");
		old.setCardId("");
		old.setCstName("张先生");

		doit("Ｄ没有明源GUID，没有身份证；A有身份证,姓氏相同", c, old);
	}

	//-----------------------------------------------------------------
	private static void testHasGUIDOver3MHasCardAHasCardNameSame() {
		CstInfo c = new CstInfo();
		c.setMobile("13812345678");
		c.setCardId("511025198603024066");
		c.setCstName("张某某");
		c.setGender(2);
		c.setOriGUID("0C364EFF-D720-4E69-BD79-280A40A73F70");
		long now = System.currentTimeMillis();
		c.setLastModifyTime(new Timestamp(now));

		CstInfo old = new CstInfo();
		old.setOriGUID("E9704087-13D9-42F2-8BA9-4EF696124A27");
		old.setLastModifyTime(new Timestamp(now - 93 * 24 * 3600 * 1000L));
		old.setMobile("13812345678");
		old.setCardId("350722198812310016");
		old.setCstName("张先生");
		old.setGender(1);

		doit("Ｄ有明源GUID，A与D更新相隔超过3个月，Ｄ有身份证，Ａ有身份证，姓氏相同", c, old);
	}

	private static void testHasGUIDOver3MNoCardAHasCardNameSame() {
		CstInfo c = new CstInfo();
		c.setMobile("13812345678");
		c.setCardId("511025198603024066");
		c.setCstName("张某某");
		c.setGender(2);
		c.setOriGUID("0C364EFF-D720-4E69-BD79-280A40A73F70");
		long now = System.currentTimeMillis();
		c.setLastModifyTime(new Timestamp(now));

		CstInfo old = new CstInfo();
		old.setOriGUID("E9704087-13D9-42F2-8BA9-4EF696124A27");
		old.setLastModifyTime(new Timestamp(now - 93 * 24 * 3600 * 1000L));
		old.setMobile("13812345678");
		old.setCardId("");
		old.setCstName("张先生");
		old.setGender(1);

		doit("Ｄ有明源GUID，A与D更新相隔超过3个月，Ｄ没有身份证，Ａ有身份证，姓氏相同", c, old);
	}

	private static void testHasGUIDOver3MHasCardANoCardNameSame() {
		CstInfo c = new CstInfo();
		c.setMobile("13812345678");
		c.setCardId("");
		c.setCstName("张某某");
		c.setGender(2);
		c.setOriGUID("0C364EFF-D720-4E69-BD79-280A40A73F70");
		long now = System.currentTimeMillis();
		c.setLastModifyTime(new Timestamp(now));

		CstInfo old = new CstInfo();
		old.setOriGUID("E9704087-13D9-42F2-8BA9-4EF696124A27");
		old.setLastModifyTime(new Timestamp(now - 93 * 24 * 3600 * 1000L));
		old.setMobile("13812345678");
		old.setCardId("350722198812310016");
		old.setCstName("张先生");
		old.setGender(1);

		doit("Ｄ有明源GUID，A与D更新相隔超过3个月，Ｄ有身份证，Ａ没有身份证，姓氏相同", c, old);
	}

	private static void testHasGUIDOver3MNoCardANoCardNameSame() {
		CstInfo c = new CstInfo();
		c.setMobile("13812345678");
		c.setCardId("");
		c.setCstName("张某某");
		c.setGender(2);
		c.setOriGUID("0C364EFF-D720-4E69-BD79-280A40A73F70");
		long now = System.currentTimeMillis();
		c.setLastModifyTime(new Timestamp(now));

		CstInfo old = new CstInfo();
		old.setOriGUID("E9704087-13D9-42F2-8BA9-4EF696124A27");
		old.setLastModifyTime(new Timestamp(now - 93 * 24 * 3600 * 1000L));
		old.setMobile("13812345678");
		old.setCardId("");
		old.setCstName("张先生");
		old.setGender(1);

		doit("Ｄ有明源GUID，A与D更新相隔超过3个月，D没有身份证，Ａ没有身份证，姓氏相同", c, old);
	}

	private static void testHasGUIDIn3MHasCardANoCardNameNotSame() {

		CstInfo c = new CstInfo();
		c.setMobile("13812345678");
		c.setCardId("");
		c.setCstName("李三儿");
		c.setGender(2);
		c.setOriGUID("0C364EFF-D720-4E69-BD79-280A40A73F70");
		long now = System.currentTimeMillis();
		c.setLastModifyTime(new Timestamp(now));

		CstInfo old = new CstInfo();
		old.setOriGUID("E9704087-13D9-42F2-8BA9-4EF696124A27");
		old.setLastModifyTime(new Timestamp(now - 80 * 24 * 3600 * 1000L));
		old.setMobile("13812345678");
		old.setCardId("511025198603024066");
		old.setCstName("张先生");
		old.setGender(1);

		doit("Ｄ有明源GUID，A与D更新相隔不超过3个月；Ｄ有身份证,Ａ没有身份证；姓氏不同", c, old);
	}

	private static void testHasGUIDIn3MHasCardANoCardNameSame() {

		CstInfo c = new CstInfo();
		c.setMobile("13812345678");
		c.setCardId("");
		c.setCstName("张三儿");
		c.setGender(2);
		c.setOriGUID("0C364EFF-D720-4E69-BD79-280A40A73F70");
		long now = System.currentTimeMillis();
		c.setLastModifyTime(new Timestamp(now));

		CstInfo old = new CstInfo();
		old.setOriGUID("E9704087-13D9-42F2-8BA9-4EF696124A27");
		old.setLastModifyTime(new Timestamp(now - 80 * 24 * 3600 * 1000L));
		old.setMobile("13812345678");
		old.setCardId("511025198603024066");
		old.setCstName("张先生");
		old.setGender(1);

		doit("Ｄ有明源GUID，A与D更新相隔不超过3个月；Ｄ有身份证,Ａ没有身份证；姓氏相同", c, old);
	}

	private static void testHasGUIDIn3MHasCardAHasCardNameNotSame() {

		CstInfo c = new CstInfo();
		c.setMobile("13812345678");
		c.setCardId("350722198812310016");
		c.setCstName("李三儿");
		c.setGender(2);
		c.setOriGUID("0C364EFF-D720-4E69-BD79-280A40A73F70");
		long now = System.currentTimeMillis();
		c.setLastModifyTime(new Timestamp(now));

		CstInfo old = new CstInfo();
		old.setOriGUID("E9704087-13D9-42F2-8BA9-4EF696124A27");
		old.setLastModifyTime(new Timestamp(now - 80 * 24 * 3600 * 1000L));
		old.setMobile("13812345678");
		old.setCardId("511025198603024066");
		old.setCstName("张先生");
		old.setGender(1);

		doit("Ｄ有明源GUID，A与D更新相隔不超过3个月；Ｄ有身份证,Ａ有身份证；姓氏不同", c, old);
	}

	private static void testHasGUIDIn3MHasCardAHasCardNameSame() {

		CstInfo c = new CstInfo();
		c.setMobile("13812345678");
		c.setCardId("350722198812310016");
		c.setCstName("张三儿");
		c.setGender(2);
		c.setOriGUID("0C364EFF-D720-4E69-BD79-280A40A73F70");
		long now = System.currentTimeMillis();
		c.setLastModifyTime(new Timestamp(now));

		CstInfo old = new CstInfo();
		old.setOriGUID("E9704087-13D9-42F2-8BA9-4EF696124A27");
		old.setLastModifyTime(new Timestamp(now - 80 * 24 * 3600 * 1000L));
		old.setMobile("13812345678");
		old.setCardId("511025198603024066");
		old.setCstName("张先生");
		old.setGender(1);

		doit("Ｄ有明源GUID，A与D更新相隔不超过3个月；Ｄ有身份证,Ａ有身份证；姓氏相同", c, old);
	}

	private static void testHasGUIDIn3MNoCardANoCardNameNotSame() {

		CstInfo c = new CstInfo();
		c.setMobile("13812345678");
		c.setCardId("");
		c.setCstName("李三儿");
		c.setGender(2);
		c.setOriGUID("0C364EFF-D720-4E69-BD79-280A40A73F70");
		long now = System.currentTimeMillis();
		c.setLastModifyTime(new Timestamp(now));

		CstInfo old = new CstInfo();
		old.setOriGUID("E9704087-13D9-42F2-8BA9-4EF696124A27");
		old.setLastModifyTime(new Timestamp(now - 80 * 24 * 3600 * 1000L));
		old.setMobile("13812345678");
		old.setCardId("");
		old.setCstName("张先生");
		old.setGender(1);

		doit("Ｄ有明源GUID，A与D更新相隔不超过3个月；Ｄ没有身份证,Ａ没有身份证；姓氏不同", c, old);
	}

	private static void testHasGUIDIn3MNoCardANoCardNameSame() {

		CstInfo c = new CstInfo();
		c.setMobile("13812345678");
		c.setCardId("");
		c.setCstName("张三儿");
		c.setGender(2);
		c.setOriGUID("0C364EFF-D720-4E69-BD79-280A40A73F70");
		long now = System.currentTimeMillis();
		c.setLastModifyTime(new Timestamp(now));

		CstInfo old = new CstInfo();
		old.setOriGUID("E9704087-13D9-42F2-8BA9-4EF696124A27");
		old.setLastModifyTime(new Timestamp(now - 80 * 24 * 3600 * 1000L));
		old.setMobile("13812345678");
		old.setCardId("");
		old.setCstName("张先生");
		old.setGender(1);

		doit("Ｄ有明源GUID，A与D更新相隔不超过3个月；Ｄ没有身份证,Ａ没有身份证；姓氏相同", c, old);
	}

	private static void testHasGUIDIn3MNoCardAHasCardNameNotSame() {

		CstInfo c = new CstInfo();
		c.setMobile("13812345678");
		c.setCardId("350722198812310016");
		c.setCstName("李三儿");
		c.setGender(2);
		c.setOriGUID("0C364EFF-D720-4E69-BD79-280A40A73F70");
		long now = System.currentTimeMillis();
		c.setLastModifyTime(new Timestamp(now));

		CstInfo old = new CstInfo();
		old.setOriGUID("E9704087-13D9-42F2-8BA9-4EF696124A27");
		old.setLastModifyTime(new Timestamp(now - 80 * 24 * 3600 * 1000L));
		old.setMobile("13812345678");
		old.setCardId("");
		old.setCstName("张先生");
		old.setGender(1);

		doit("Ｄ有明源GUID，A与D更新相隔不超过3个月；Ｄ没有身份证,Ａ有身份证；姓氏不同", c, old);
	}

	private static void testHasGUIDIn3MNoCardAHasCardNameSame() {

		CstInfo c = new CstInfo();
		c.setMobile("13812345678");
		c.setCardId("350722198812310016");
		c.setCstName("张三儿");
		c.setGender(2);
		c.setOriGUID("0C364EFF-D720-4E69-BD79-280A40A73F70");
		long now = System.currentTimeMillis();
		c.setLastModifyTime(new Timestamp(now));

		CstInfo old = new CstInfo();
		old.setOriGUID("E9704087-13D9-42F2-8BA9-4EF696124A27");
		old.setLastModifyTime(new Timestamp(now - 80 * 24 * 3600 * 1000L));
		old.setMobile("13812345678");
		old.setCardId("");
		old.setCstName("张先生");
		old.setGender(1);

		doit("Ｄ有明源GUID，A与D更新相隔不超过3个月；Ｄ没有身份证,Ａ有身份证；姓氏相同", c, old);
	}

	static void testShouldMerge() {
		System.out.println("A：表示从明源同过来的最新记录");
		System.out.println("D：表示数据中心已存在的旧记录");
		System.out.print("-------------------------------");
		System.out.print("-------------------------------");
		System.out.println("-------------------------------");

		// 旧档没有明源GUID，没身份证，四种场景
		testNoGUIDNoCardANoCardNameNotSame();	// 
		testNoGUIDNoCardANoCardNameSame();		//
		testNoGUIDNoCardAHasCardNameNotSame();	//
		testNoGUIDNoCardAHasCardNameSame();		//

		// 旧档没有明源GUID，有身份证，四种场景
		testNoGUIDHasCardAHasCardNameNotSame();
		testNoGUIDHasCardAHasCardNameSame();
		testNoGUIDHasCardANoCardNameNotSame();
		testNoGUIDHasCardANoCardNameSame();

		// 更新间隔超3个月，四种场景
		testHasGUIDOver3MHasCardAHasCardNameSame();
		testHasGUIDOver3MNoCardAHasCardNameSame();
		testHasGUIDOver3MHasCardANoCardNameSame();
		testHasGUIDOver3MNoCardANoCardNameSame();

		// 更新间隔不超3个月，旧档有身份证，四种场景
		testHasGUIDIn3MHasCardANoCardNameNotSame();
		testHasGUIDIn3MHasCardANoCardNameSame();
		testHasGUIDIn3MHasCardAHasCardNameNotSame();
		testHasGUIDIn3MHasCardAHasCardNameSame();

		// 更新间隔不超3个月，旧档没有身份证，四种场景
		testHasGUIDIn3MNoCardANoCardNameNotSame();
		testHasGUIDIn3MNoCardANoCardNameSame();
		testHasGUIDIn3MNoCardAHasCardNameNotSame();
		testHasGUIDIn3MNoCardAHasCardNameSame();
	}

	static void updateOrderFollowRecord() throws SQLException {
		Connection con = ADataSource.getConnection(ADataSource.SCHEMA_BIZ);
		Map<String, RoomInfo> roomInfos = new HashMap<String, RoomInfo>();
		{
			Statement stmt = null;
			ResultSet rs = null;
			System.out.println("query order room info");
			String sql = "select r.RoomGUID,r.Room,r.Huxing,o.TradeGUID from p_Room r join s_Order o on r.RoomGUID=o.RoomGUID";
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			System.out.println("cache order room info");
			while (rs.next()) {
				RoomInfo ri = new RoomInfo();
				ri.setRoomGUID(rs.getString(1));
				ri.setRoom(rs.getString(2));
				ri.setHuxing(rs.getString(3));
				roomInfos.put(rs.getString(4), ri);
			}
			System.out.println("room cache done");
		}

		List<String> tables = Cache.loadTables(con, "cst_followrecord_");
		System.out.println("update tables:" + tables);
		for (String table : tables) {
			String sql = "update "
					+ table
					+ " set RoomGUID=?,Room=?,Huxing=? where followType=5 and TradeGUID=?";
			PreparedStatement updatePstmt = con.prepareStatement(sql);
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery("select TradeGUID from " + table
					+ " where followType=5");
			try {
				int count = 0;
				while (rs.next()) {
					String tradeGUID = rs.getString(1);
					RoomInfo ri = roomInfos.get(tradeGUID);
					if (ri == null) {
						continue;
					}
					count++;
					updatePstmt.setString(1, ri.getRoomGUID());
					updatePstmt.setString(2, ri.getRoom());
					updatePstmt.setString(3, ri.getHuxing());
					updatePstmt.setString(4, tradeGUID);
					updatePstmt.addBatch();
					if (count % 2000 == 0) {
						updatePstmt.executeBatch();
						updatePstmt.clearBatch();
						System.out.println(count + " order follow updated");
					}
				}
				updatePstmt.executeBatch();
				updatePstmt.clearBatch();
				System.out.println("on tables done[" + table + "]");
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				ADataSource.close(updatePstmt);
				ADataSource.close(rs, stmt, null);
			}
		} // end 
		roomInfos.clear();
	} //~

	static void updateContractFollowRecord() throws SQLException {
		Connection con = ADataSource.getConnection(ADataSource.SCHEMA_BIZ);
		Map<String, RoomInfo> roomInfos = new HashMap<String, RoomInfo>();
		{
			Statement stmt = null;
			ResultSet rs = null;
			System.out.println("query contract room info");
			String sql = "select r.RoomGUID,r.Room,r.Huxing,o.TradeGUID from p_Room r join s_Contract o on r.RoomGUID=o.RoomGUID";
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			System.out.println("cache contract room info");
			while (rs.next()) {
				RoomInfo ri = new RoomInfo();
				ri.setRoomGUID(rs.getString(1));
				ri.setRoom(rs.getString(2));
				ri.setHuxing(rs.getString(3));
				roomInfos.put(rs.getString(4), ri);
			}
			System.out.println("room cache done");
		}

		List<String> tables = Cache.loadTables(con, "cst_followrecord_");
		System.out.println("update tables:" + tables);
		for (String table : tables) {
			String sql = "update "
					+ table
					+ " set RoomGUID=?,Room=?,Huxing=? where followType=6 and TradeGUID=?";
			PreparedStatement updatePstmt = con.prepareStatement(sql);
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery("select TradeGUID from " + table
					+ " where followType=6");
			try {
				int count = 0;
				while (rs.next()) {
					String tradeGUID = rs.getString(1);
					RoomInfo ri = roomInfos.get(tradeGUID);
					if (ri == null) {
						continue;
					}
					count++;
					updatePstmt.setString(1, ri.getRoomGUID());
					updatePstmt.setString(2, ri.getRoom());
					updatePstmt.setString(3, ri.getHuxing());
					updatePstmt.setString(4, tradeGUID);
					updatePstmt.addBatch();
					if (count % 2000 == 0) {
						updatePstmt.executeBatch();
						updatePstmt.clearBatch();
						System.out.println(count + " contract follow updated");
					}
				}
				updatePstmt.executeBatch();
				updatePstmt.clearBatch();
				System.out.println("on tables done[" + table + "]");
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				ADataSource.close(updatePstmt);
				ADataSource.close(rs, stmt, null);
			}
		} // end 
	} //~

	static void updateMappingList(Connection con, String table) {
		int total = 0;
		long last = 0;
		while (true) {
			try {
				PreparedStatement pstmt = null;
				ResultSet rs = null;
				String sql = "select CstGUID from "
						+ table
						+ " where OriGUIDList is null or OriGUIDList='' and CstGUID>"
						+ last + " order by CstGUID asc limit 100";
				pstmt = con.prepareStatement(sql);
				rs = pstmt.executeQuery();
				Set<Long> set = new HashSet<Long>();
				if (!rs.next()) {
					return;
				}
				int batch = 0;
				do {
					total++;
					batch++;
					last = rs.getLong(1);
					set.add(last);
				} while (rs.next());
				JSONObject json = new JSONObject();
				json.put("cmd", "update_mapping");
				json.put("ids", set);
				System.out.println("更新已同步客户映射");
				MappingHandler mappingHandler = new MappingHandler();
				System.out.println(mappingHandler.handle(json));
				ADataSource.close(rs, pstmt, null);
				if (batch < 100) {
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
			}
			System.out.println(total + " updated");
		}
	}

	public static void main(String[] args) throws SQLException {
		Connection con = ADataSource.getConnection(ADataSource.SCHEMA_BIZ);
		String[] tables = { "cst_Customer_141", "cst_Customer_142",
				"cst_Customer_143", "cst_Customer_144", "cst_Customer_145",
				"cst_Customer_146", "cst_Customer_147" };
		for (String table : tables) {
			updateMappingList(con, table);
		}
		ADataSource.close(con);
		//		updateOrderFollowRecord();
		//		updateContractFollowRecord();
		//		int state = TradeUtil.getTradeState("");
		//		System.out.println(state);
	} //~main
}
